package com.dm.biz.item.service.impl;

import com.dm.biz.item.client.DmImageClient;
import com.dm.biz.item.dao.DmItemDao;
import com.dm.biz.item.pojo.DmItem;
import com.dm.biz.item.service.DmItemService;
import com.dm.biz.item.vo.BannerVo;
import com.dm.biz.item.vo.DmImage;
import com.dm.comm.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("dmItemService")
@Slf4j
public class DmItemServiceImpl implements DmItemService {

    @Resource
    private DmItemDao dmItemDao;

    @Resource
    private DmImageClient dmImageClient;

    @Override
    public List<BannerVo> searchBanner() {
        List<BannerVo> bannerVos=new ArrayList<>();
        List<DmItem> list=dmItemDao.findTop5ByIsBannerOrderByUpdatedTimeDesc(1L);
        list.forEach(dmItem -> {
            BannerVo vo=new BannerVo();
            vo.setId(dmItem.getId());
            vo.setItemName(dmItem.getItemName());
            vo.setMinPrice(dmItem.getMinPrice());
            Result<List<DmImage>> result=dmImageClient.query(dmItem.getId(),1L,1L);
            List<DmImage> dmImages=result.getData();
            String imgUrl=dmImages.get(0).getImgUrl();
             vo.setImgUrl(imgUrl);
            bannerVos.add(vo);
        });
        return bannerVos;
    }
}
